///*
// * DetectMove.cpp
// *
// *  Created on: Nov 14, 2013
// *      Author: thanhkm
// */
//
//#include <opencv2/opencv.hpp>
//#include <iostream>
//
//using namespace cv;
//using namespace std;
//
////list history
//cv::Mat histoFrame[10];
//// current gray-level image
//cv::Mat gray;
//// accumulated background
//cv::Mat background;
//// background image
//cv::Mat backImage;
//// foreground image
//cv::Mat foreground;
//// learning rate in background accumulation
//double learningRate = 0.01;
//int thresh = 30;
//RNG rng(12345);
//cv::BackgroundSubtractorMOG mog;
//
//void process(cv::Mat &frame, cv::Mat &output);
//void drawBoxImage(Mat img);
//float dist(Point a, Point b);
//void getForeGround(cv::Mat &frame, cv::Mat &output);
//
//
//int main(int argc, char** argv) {
//	VideoCapture cap;
//	//open video or webcam
//	if (argc > 1)
//		cap.open(argv[1]);
//	else
//		cap.open(0);
//	// check open
//	if (!cap.isOpened()) {
//		std::cerr
//				<< "Cannot read video. Try moving video file to sample directory."
//				<< std::endl;
//		return -1;
//	}
//
//	Mat frame;
//	Mat foreground;
//
//	for (;;) {
//		cap >> frame;
//		if(frame.empty())
//			break;
//		process(frame, foreground);
//		imshow("video", frame);
//		imshow("foreground", foreground);
//
//		drawBoxImage(foreground);
//
//
//		// for quit
//		int c = waitKey(30);
//		if (c == 'q' || c == 'Q' || (c & 255) == 27)
//			break;
//	}
//	return 0;
//}
//
//// processing method
//void process(cv::Mat &frame, cv::Mat &output) {
//	// convert to gray-level image
//	cv::cvtColor(frame, gray, CV_BGR2GRAY);
//	// initialize background to 1st frame
//	if (background.empty()){
//		gray.convertTo(background, CV_32F);
//	}
//	// convert background to 8U
//	background.convertTo(backImage, CV_8U);
//	// compute difference between image and background
//	cv::absdiff(backImage, gray, foreground);
//	// apply threshold to foreground image
//	cv::threshold(foreground, output, thresh, 255, cv::THRESH_BINARY);
////	cv::threshold(foreground, output, thresh, 0, cv::THRESH_TOZERO);
////		foreground.copyTo(output);
//
//	erode(output, output, Mat(), Point(-1, -1), 2);
//	dilate(output, output, Mat(), Point(-1, -1), 1);
////	erode(output, output, Mat(), Point(-1, -1), 2);
////	dilate(output, output, Mat(), Point(-1, -1), 1);
//
//	// accumulate background
//	cv::accumulateWeighted(gray, background, learningRate, output);
//}
//
//void getForeGround(cv::Mat &frame, cv::Mat &output) {
//	// convert to gray-level image
//	cv::cvtColor(frame, gray, CV_BGR2GRAY);
//	// initialize background to 1st frame
//	if (background.empty()){
//		gray.convertTo(background, CV_32F);
//	}
//
//	mog(gray, output,0.01);
//}
//
//void drawBoxImage(Mat img)
//{
//  Mat threshold_output;
//  vector<vector<Point> > contours;
//  vector<Vec4i> hierarchy;
//
//  /// Detect edges using Threshold
//  threshold( img, threshold_output, thresh, 255, THRESH_BINARY );
//  /// Find contours
//  findContours( threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
//
//  /// Approximate contours to polygons + get bounding rects and circles
//  vector<vector<Point> > contours_poly( contours.size() );
//  vector<Rect> boundRect( contours.size() );
//  vector<Point2f>center( contours.size() );
//  vector<float>radius( contours.size() );
//
//  for( int i = 0; i < contours.size(); i++ )
//     { approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true );
//       boundRect[i] = boundingRect( Mat(contours_poly[i]) );
//       minEnclosingCircle( (Mat)contours_poly[i], center[i], radius[i] );
//     }
//
//
//  /// Draw polygonal contour + bonding rects + circles
//  Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 );
//  for( int i = 0; i< contours.size(); i++ )
//     {
//       Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
////       drawContours( drawing, contours_poly, i, color, 1, 8, vector<Vec4i>(), 0, Point() );
//       if (dist(boundRect[i].tl(), boundRect[i].br())>10){
//       rectangle( drawing, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0 );
////       circle( drawing, center[i], (int)radius[i], color, 2, 8, 0 );
//       }
//     }
//
//  /// Show in a window
//  namedWindow( "Contours", CV_WINDOW_AUTOSIZE );
//  imshow( "Contours", drawing );
//}
//
//float dist(Point a, Point b){
//	return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
//}
